package com.jinyu.jyly.ui.mine.real_name;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.jinyu.jyly.R;
import com.jinyu.jyly.base.BaseActivity;
import com.jinyu.jyly.bean.AllInfoBean;
import com.jinyu.jyly.utils.AES;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 实名认证
 */
public class RealNameActivity extends BaseActivity<RealNamePresenter> implements RealNameContract.View {

    @BindView(R.id.fake_status_bar)
    View fakeStatusBar;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.rl_title_view)
    RelativeLayout rlTitleView;
    @BindView(R.id.btn_sub)
    Button btnSub;
    @BindView(R.id.et_real_name)
    EditText etRealName;
    @BindView(R.id.et_id_num)
    EditText etIdNum;
    private RealNamePresenter presenter;

    @Override
    public int getLayout() {
        return R.layout.activity_real_auth_name;
    }

    @Override
    protected RealNamePresenter getPresenter() {
        if (presenter == null) {
            presenter = new RealNamePresenter(this, this);
        }
        return presenter;
    }

    @Override
    protected void initEventAndData() {
        QMUIStatusBarHelper.translucent(this);
        QMUIStatusBarHelper.setStatusBarDarkMode(this);
        Intent intent = getIntent();
        String card_name = intent.getStringExtra("card_name");
        String card_num = intent.getStringExtra("card_num");
        if(!TextUtils.isEmpty(card_name)){
            etRealName.setText(card_name);
            etIdNum.setText(card_num);
            btnSub.setVisibility(View.GONE);
            etRealName.setFocusable(false);
            etRealName.setFocusableInTouchMode(false);
            etIdNum.setFocusable(false);
            etIdNum.setFocusableInTouchMode(false);
        }else{

        }
    }

    /**
     * 认证
     * @param info
     */
    @Override
    public void onAuthRealName(AllInfoBean info) {
        switch (info.getStatus()){
            case 200:
                showToast("认证成功");
                finish();
                break;
            case 422:
                showToast(info.getMsg());
                break;
        }
    }

    @Override
    public void onFailer(Throwable throwable) {

    }

    @OnClick({R.id.iv_back, R.id.btn_sub})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.btn_sub:
                String realName = etRealName.getText().toString();
                String realId = etIdNum.getText().toString();
                if(TextUtils.isEmpty(realName)){
                    showToast("身份证不能为空");
                    return;
                }

                 if(TextUtils.isEmpty(realId)){
                    showToast("身份证号不能为空");
                    return;
                }

                try {
                    //验证码
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("name", realName);
                    jsonObject.put("num", realId);
                    String param = AES.encryptToHexString(jsonObject.toString());
                    presenter.authRealName(param);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
            default:
                break;
        }
    }


}
